package com.atguigu.edu.service.impl;

import com.atguigu.edu.entity.Chapter;
import com.atguigu.edu.entity.Video;
import com.atguigu.edu.entity.chapter.ChapterVo;
import com.atguigu.edu.entity.chapter.VideoVo;
import com.atguigu.edu.mapper.ChapterMapper;
import com.atguigu.edu.service.ChapterService;
import com.atguigu.edu.service.VideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Rayyy666
 * @since 2022-04-02
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService; //注入小节service

    /**
     * 根据课程id，查询课程的章节，小节信息
     *
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //1.根据课程id，查询课程里面所有的章节
        QueryWrapper<Chapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id", courseId);
        List<Chapter> chapterList = baseMapper.selectList(chapterWrapper);

        //2.根据课程id，查询所有小节
        QueryWrapper<Video> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id", courseId);
        List<Video> videoList = videoService.list(videoWrapper);

        //创建list集合，用于封装最终的数据
        List<ChapterVo> finalList = new ArrayList<>();

        //3.遍历查询章节list集合进行封装
        //遍历查询章节list集合
        for (int i = 0; i < chapterList.size(); i++) {
            //每个章节
            Chapter chapter = chapterList.get(i);
            //把 chapter 里面的值对应复制到ChapterVo里面
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter, chapterVo);

            //把ChapterVo放到最终数据集合中finalList
            finalList.add(chapterVo);
            //创建集合，用于封装该章节下的小节
            List<VideoVo> videoVoList = new ArrayList<>();

            //4.遍历查询小节list集合，进行封装
            for (int j = 0; j < videoList.size(); j++) {
                //得到每个小节
                Video video = videoList.get(j);
                //判断：小节中的chapterId 和章节里面的id是否一样
                if (video.getChapterId().equals(chapter.getId())) {
                    //进行封装
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);
                    //放到小节集合的封装中
                    videoVoList.add(videoVo);
                }
            }
            //把最终封装的小节集合 放到章节里面去
            chapterVo.setChildren(videoVoList);
        }

        return finalList;
    }

    /**
     * 删除章节
     *
     * @param chapterId
     * @return
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapterId 查询小节表，如果有数据，不能删除，没有则可以删除
        QueryWrapper<Video> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("chapter_id", chapterId);
        int count = videoService.count(videoWrapper);

        //判断是否有小节数据
        if (count == 0) {
            //不能查询出数据 可以删除
            int result = baseMapper.deleteById(chapterId);

            //成功就是1， 失败就是0
            return result > 0;
        } else {
            //能查询出数据，不可以删除
            throw new GuliException(20001, "有数据不能删除");
        }
    }

    /**
     * 根据课程ID 删除章节
     *
     * @param courseId
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();


        wrapper.eq("course_id", courseId);
//        int i = 10 / 0;
        baseMapper.delete(wrapper);
    }
}
